package com.tanhua.server.controller;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.VideoComment;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import com.tanhua.server.service.VideoCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.ldap.StartTlsRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private VideoCommentService videoCommentService;


    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }
    /**
     * 点赞
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId) {
         videoCommentService.likeComment(videoId);
        return ResponseEntity.ok(null);
    }
    /**
     * 取消点赞
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String videoId) {
        videoCommentService.unlikeComment(videoId);
        return ResponseEntity.ok(null);
    }
    /**
     * 关注
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid) {
        // 1.当前视频的发布者的ID
        videosService.userFocus(uid);
        // 2.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long uid) {
        // 1.当前视频的发布者的ID
        videosService.userFocus(uid);
        // 2.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity subComments(@RequestBody Map comment){
        String id = (String) comment.get("id");
        String comment1 = (String) comment .get("comment");
        videosService.subComments(id,comment1);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity listComments(@RequestParam(defaultValue = "1")  Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       @PathVariable("id") String id){
       PageResult pageResult =  videosService.listComments(id,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike( @PathVariable("id") String commentId){
        videosService.commentsLike(commentId);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDisLike( @PathVariable("id") String commentId){
        videosService.commentsDisLike(commentId);
        return ResponseEntity.ok(null);
    }
}